<?php
class UserAction extends Action{
    //登录逻辑
    function login(){
		$userlogin=$this->checklogin();
		$this->assign('userlogin',$userlogin);
        $this->display();
    }
    //验证登录逻辑
    function checkuser(){
        if(isset($_POST['login'])){
        	if($_SESSION['verify']==md5($_POST['verify'])){
	        	//创建用户模型
	        	$user=M('Userbase');
	        	//用户查询条件
	        	$data['username']=array('eq',$_POST['username']);
				//查询相应的用户
	        	$userinfo=$user->where($data)->find();
	        	//判断有没有用户
	        	if(is_array($userinfo)){
	        		if($userinfo['password']==md5($_POST['password'])){
	        			Cookie::set('userid_name',$userinfo['username'].'-'.$userinfo['id']);
	        			Cookie::set('passwdcode',md5($userinfo['username'].$userinfo['password'].$userinfo['id'].C('COOKIE_PASS')));
	        			$this->assign('pageTitle',L('_LOGIN_SUCCESS_'));
	        			$this->assign('msgTitle',L('_LOGIN_SUCCESS_'));
	        			$this->success(L('_LOGIN_SUCCESS_'));
	        		}else{
	        			$this->assign('pageTitle',L('_LOGIN_FIELD_'));
	        			$this->assign('msgTitle',L('_LOGIN_FIELD_'));
	        			$this->error(L('_LOGIN_FIELD_EPASS_'));
	        		}
	        	}else{
	        		$this->assign('pageTitle',L('_LOGIN_FIELD_'));
	        		$this->assign('msgTitle',L('_LOGIN_FIELD_'));
	        		$this->error(L('_LOGIN_FIELD_NOUSER_'));
	        	}
        	}else{
	        	$this->assign('pageTitle',L('_LOGIN_FIELD_'));
		        $this->assign('msgTitle',L('_LOGIN_FIELD_'));
		        $this->error(L('_LOGIN_FIELD_ECODE_'));
        	}
        }
    }
    
    function code(){
        import("ORG.Util.Image");
        import("ORG.Util.String");
        Image::buildImageVerify();
        
    }

    static function checklogin($showerror=false){
    	//获得Cookie中保存的用户ID及用户名信息
    	$userid_name=Cookie::get('userid_name');
    	//获得用户名和用户ID
    	$userid_name=explode("-", $userid_name);
    	$userid=$userid_name[1];
    	$username=$userid_name[0];
    	//获得Cookie中保存的加密信息
    	$passwdcode=Cookie::get('passwdcode');
    	//判断用户登陆情况
    	$user=M('Userbase');
    	$data['id']=array('eq',$userid);
    	$userinfo=$user->where($data)->find();
    	$checkpasswdcode=md5($userinfo['username'].$userinfo['password'].$userinfo['id'].C('COOKIE_PASS'));
    	if(is_array($userinfo)){
    		
    		if($passwdcode==$checkpasswdcode){
    			return true;
    		}else{
    			$this->error('_ERROR_ACTION_');
    			return false;
    		}
    	}elseif($showerror==true){
    		$this->error('_NOT_LOGIN_');
    		return false;
    	}
    }


}
?>